package com.JavaBasic;

public class StringBufferDemo {
	public static void main(String[] args) {
		// StringBuffer构造
		StringBuffer sBuffer1 = new StringBuffer();
		StringBuffer sBuffer2 = new StringBuffer(10);
		StringBuffer sb = new StringBuffer("Hello");
		System.out.println("原字符串:" + sb);
		
		// 获取StringBuffer长度
		// capacity()获取StringBuffer容器的大小
		// length()获取StringBuffer里实际字符长度
		int cap = sb.capacity();
		int len = sb.length();
		System.out.println("获取StringBuffer容器大小:" + cap);
		System.out.println("获取StringBuffer内实际字符长度:" + len);
		
		// StringBuffer的追加
		// append()追加任意类型数据到字符串后面
		
		sb.append("123");
		System.out.println("追加字符串:" + sb);
		
		// StringBuffer插入
		// insert()插入数据到指定的索引
		sb.insert(1, "world");
		System.out.println("插入数据到指定的索引:" + sb);
		
		// StringBuffer删除功能
		sb.deleteCharAt(2);
		System.out.println("删除单个字符:" + sb);
		sb.delete(1, 5);
		System.out.println("删除区间内所有字符:" + sb);
		
		// StringBuffer替换和反转
		sb.replace(5, 8, "world");
		System.out.println("替换：" + sb);
		sb.reverse();
		System.out.println("反转:" + sb);
		
		 // StringBuffer截取功能
		String string = sb.substring(1);
		System.out.println("从指定index截取到字符串结尾:" + string);
		String s = sb.substring(3, 8);
		System.out.println("截取指定区间字符:" + s);
		
		// StringBuffer转换成String
		String s1 = sb.substring(0);
		String s2 = sb.toString();
		String s3 = new String(sb);
		System.out.println("StringBuffer转换成String:" + s1 + "," + s2 + "," + s3 + ",");
	}
}
